package com.wkbb.common.utils;

import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

public class RSAUtil {
    private static String publicKeyString = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALaf+HKXnSmXQHngENYyfEfcgQta/KVNRHhsLUsXEeK47J0217YUAcP8JDi3LPufWxZqmcBmphQw6eMJ6YgLnX0CAwEAAQ==";
    private static String privateKeyString = "MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAtp/4cpedKZdAeeAQ1jJ8R9yBC1r8pU1EeGwtSxcR4rjsnTbXthQBw/wkOLcs+59bFmqZwGamFDDp4wnpiAudfQIDAQABAkEAgVCJFmtxJ/AoAaS9h068Xg4veiD1YP9zimnfMJqnWYgyHP9/H2LtcR1+W0Ej+Feu/pPuU0QBGpfdbHSIL3pbwQIhANht2z6f6cpuM6uMYKYc/8gzr//369534nJ+XWIUrpNVAiEA2APg8V5EAcIiMfJ6WCtnfxRzJUodvzjfCx/F0wm9sYkCIHa3w/TVKRwWWRd3uChGbfP2jktdRi/RGRDuyfCcIarFAiEAmXtgB3+H6IvsSfrUbPMiDUwVaEhaDHU3HStNHt6L9qkCIQCnBH2fBGWXoRXOtHoFPuGMGpanrFgHPskobqG1C8vOfw==";
    private static PublicKey publicKey;
    private static PrivateKey privateKey;

    static {
        try {
            publicKey = string2PublicKey(publicKeyString);
            privateKey = string2PrivateKey(privateKeyString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {


        String str = "yinshi3634";
       String messageEn = null;
        try {
//            messageEn = publicEncrypt(str);
//            System.out.println("加密后的字符串为:" + messageEn);
            String messageDe = privateDecrypt("K0MfOYffiC3pWb+NHH31O36GyfYjLNtmLygHn5Z1r6wBcyfowKn8VQteHAfnmqGQaerYO8LaU4U/jNkJGZBrMw==");
            System.out.println("还原后的字符串为:" + messageDe);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //公钥加密
    public static String publicEncrypt(String content){
        if (content == null) return null;
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] bytes = cipher.doFinal(content.getBytes());
            return byte2Base64(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }

    //私钥解密
    public static String privateDecrypt(String content){
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] bytes = cipher.doFinal(base642Byte(content));
            return new String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }

    //生成秘钥对
    public static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(512);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }

    //获取公钥(Base64编码)
    public static String getPublicKey(KeyPair keyPair){
        PublicKey publicKey = keyPair.getPublic();
        byte[] bytes = publicKey.getEncoded();
        return byte2Base64(bytes);
    }

    //获取私钥(Base64编码)
    public static String getPrivateKey(KeyPair keyPair){
        PrivateKey privateKey = keyPair.getPrivate();
        byte[] bytes = privateKey.getEncoded();
        return byte2Base64(bytes);
    }

    //将Base64编码后的公钥转换成PublicKey对象
    public static PublicKey string2PublicKey(String pubStr) throws Exception{
        byte[] keyBytes = base642Byte(pubStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    }

    //将Base64编码后的私钥转换成PrivateKey对象
    public static PrivateKey string2PrivateKey(String priStr) throws Exception{
        byte[] keyBytes = base642Byte(priStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }

    //公钥加密
    public static String publicEncrypt(String content, String publicKey) throws Exception{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, string2PublicKey(publicKey));
        byte[] bytes = cipher.doFinal(content.getBytes());
        return byte2Base64(bytes);
    }

    //私钥解密
    public static String privateDecrypt(String content, String privateKey) throws Exception{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, string2PrivateKey(privateKey));
        byte[] bytes = cipher.doFinal(base642Byte(content));
        return new String(bytes);
    }

    //公钥加密
    public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }

    //私钥解密
    public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }

    //字节数组转Base64编码
    public static String byte2Base64(byte[] bytes){
        return Base64Util.encode(bytes);
    }

    //Base64编码转字节数组
    public static byte[] base642Byte(String base64Key) throws IOException {
        return Base64Util.decode(base64Key);
    }
}
